﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GMS.Model;
using System.Collections.Generic;//for list
using GMS.BLL;
using GMS.Utils;

/// <summary>
/// ColumnsManager 的摘要说明
/// </summary>
public class ColumnsManager
{
    
    private readonly TabColumnsBLL TCBLL = new TabColumnsBLL();
    public ColumnsManager()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    //从数据库里面获取字段信息,排过序的
    public List<TabColumns> GetTabColListOrderByColSeq(string tabCode)
    {
        //获取表的字段信息
        if (tabCode == null || tabCode == "")
            return null;
        string sqlWhereColTab = " upper(tabcode) = '" + tabCode.ToUpper() + "' ORDER BY COLSEQ";        
        return TCBLL.GetModelList(sqlWhereColTab);        
    }

    //获取字段信息
    public List<TabColumns> GetTabColList(string tabCode)
    {
        //获取表的字段信息
        if (tabCode == null || tabCode == "")
            return null;       
        return TCBLL.GetModelListByTable(tabCode);
    }

    //获取字段信息：
    public Dictionary<string, TabColumns> GetTabColDict(string tabCode)
    {
        if (tabCode == null || tabCode.Equals(""))
            return null;
        Dictionary<string, TabColumns> tabDict = new Dictionary<string, TabColumns>();
        List<TabColumns> tabList = GetTabColList(tabCode);
        for (int i = 0; i < tabList.Count; i++)
        {      
            if(!tabDict.ContainsKey(tabList[i].COLCODE.ToUpper()))
                tabDict.Add(tabList[i].COLCODE.ToUpper(), tabList[i]);
        }
        return tabDict;
    }

    //同时获取字段信息和主键
    //tabKey
    public Dictionary<string, TabColumns> GetTabColAndKeyDict(string tabCode, ref Dictionary<string, string> tabKey)
    {
        if (tabCode == null || tabCode.Equals(""))
            return null;
        Dictionary<string, TabColumns> tabDict = new Dictionary<string, TabColumns>();
        List<TabColumns> tabList = GetTabColList(tabCode.ToUpper());
        for (int i = 0; i < tabList.Count; i++)
        {
            if (!tabDict.ContainsKey(tabList[i].COLCODE.ToUpper()))
                tabDict.Add(tabList[i].COLCODE.ToUpper(), tabList[i]);

            if (tabKey != null)
            {   //1-自增主键 2-非自增主键
                if ((tabList[i].COLISKEY == (int)EnumHelper.E_ColIsKey.COL_SEQKEY || tabList[i].COLISKEY == (int)EnumHelper.E_ColIsKey.COL_INPUTKEY) 
                    && !tabKey.ContainsKey(tabList[i].COLCODE.ToUpper()))
                {
                    tabKey.Add(tabList[i].COLCODE.ToUpper(), "");
                }
            }
        }
        return tabDict;
    } 

    //获取主键信息
    public Dictionary<string, string> GetTabKeyDict(List<TabColumns> tabList)
    {        
        if (tabList == null || tabList.Count == 0)
            return null;
        Dictionary<string, string> tabKey = new Dictionary<string, string>();
        for (int i = 0; i < tabList.Count; i++)
        {
            if ((tabList[i].COLISKEY == (int)EnumHelper.E_ColIsKey.COL_SEQKEY || tabList[i].COLISKEY == (int)EnumHelper.E_ColIsKey.COL_INPUTKEY)
                && !tabKey.ContainsKey(tabList[i].COLCODE.ToUpper()))
            {
                tabKey.Add(tabList[i].COLCODE.ToUpper(), "");
            }       
        }
        return tabKey;
    }    

    public List<GMS.Model.TabColumns> DataTableToList(DataTable dt)
    {
        return  TCBLL.DataTableToList(dt);
    }

    public DataSet GetList(string strWhere)
    {
        return TCBLL.GetList(strWhere);
    }
}
